/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * bzr_vaNeuVeranstaltung.java
 *
 * Created on 17.03.2011, 09:19:36
 */
package bzr_veranstaltungverwaltung;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JSpinner;
import javax.swing.SpinnerDateModel;
import javax.swing.SpinnerModel;

/**
 *
 * @author fhiesl
 */
public class bzr_vaNeuVeranstaltung extends javax.swing.JFrame {

    public Connection con;
    public SpinnerModel model = new SpinnerDateModel();
    /** Creates new form bzr_vaNeuVeranstaltung */
    public bzr_vaNeuVeranstaltung() throws InstantiationException, IllegalAccessException, SQLException {
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            String url = "jdbc:mysql://mysql.bezirksrundschau.com:3306/brdbm_1";
            con = (Connection) DriverManager.getConnection(url, "brdbm", "br2011!db");
            System.out.println("Verbunden");         
            initComponents();
            initDatum();
            
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(bzr_vaNeuVeranstaltung.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jTextField_Name = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jTextField_Ort = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jTextField_Straße = new javax.swing.JTextField();
        jTextField_ZustandigeGeschaftsstelle = new javax.swing.JTextField();
        jSpinnerStartDatum = new javax.swing.JSpinner();
        jSpinnerEndDatum = new javax.swing.JSpinner();
        jButtonVeranstaltungAnlegen = new javax.swing.JButton();
        jFormattedTextField_plz = new javax.swing.JFormattedTextField();
        jFormattedTextField_hnr = new javax.swing.JFormattedTextField();
        jButton_schliessen = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Name");

        jLabel2.setText("Ort");

        jLabel3.setText("PLZ");

        jLabel4.setText("Straße");

        jLabel5.setText("Haus Nr.");

        jLabel6.setText("StartDatum");

        jLabel7.setText("EndDatum");

        jLabel8.setText("Zustandige Geschäftsstelle");

        jSpinnerStartDatum.setModel(model);

        jSpinnerEndDatum.setModel(new javax.swing.SpinnerDateModel());

        jButtonVeranstaltungAnlegen.setText("Veranstaltung anlegen");
        jButtonVeranstaltungAnlegen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonVeranstaltungAnlegenActionPerformed(evt);
            }
        });

        jFormattedTextField_plz.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0"))));

        jFormattedTextField_hnr.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0"))));

        jButton_schliessen.setText("Schließen");
        jButton_schliessen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_schliessenActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel8)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(4, 4, 4)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3)))
                    .addComponent(jLabel6)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5)
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jButtonVeranstaltungAnlegen)
                    .addComponent(jFormattedTextField_plz, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jFormattedTextField_hnr, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jSpinnerStartDatum, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField_ZustandigeGeschaftsstelle, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jSpinnerEndDatum, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField_Ort)
                    .addComponent(jTextField_Name, javax.swing.GroupLayout.DEFAULT_SIZE, 162, Short.MAX_VALUE)
                    .addComponent(jTextField_Straße))
                .addGap(75, 75, 75)
                .addComponent(jButton_schliessen)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextField_Name, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextField_Ort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jFormattedTextField_plz, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jTextField_Straße, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jFormattedTextField_hnr, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jSpinnerStartDatum, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(jSpinnerEndDatum, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(jTextField_ZustandigeGeschaftsstelle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(46, 46, 46)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonVeranstaltungAnlegen, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton_schliessen))
                .addContainerGap(27, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonVeranstaltungAnlegenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonVeranstaltungAnlegenActionPerformed
        // TODO add your handling code here:
        if (prüfTextFields()) {
            try {
                Date d = (Date) jSpinnerStartDatum.getValue();
                SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
                String dateString = sd.format(d);
                d = (Date) jSpinnerEndDatum.getValue();
                sd = new SimpleDateFormat("yyyy-MM-dd");
                String dateStringEnd = sd.format(d);
                Statement st = (Statement) con.createStatement();
                st.executeUpdate("INSERT INTO Veranstaltungen (Name,Ort,PLZ,Strasse, HausNr,Startdatum,Enddatum,ZustaendigeGeschaeftsstelle)"
                        + "VALUES ('" + jTextField_Name.getText() + "','" + jTextField_Ort.getText() + "'," + jFormattedTextField_plz.getText() + ",'" + jTextField_Straße.getText() + "'," + jFormattedTextField_hnr.getText() + ",'" + dateString + "','" + dateStringEnd + "','" + jTextField_ZustandigeGeschaftsstelle.getText() + "')");
                JOptionPane.showMessageDialog(this, "Veranstaltung  " + jTextField_Name.getText() + " wurde erfolgreich erstellt");
                textfieldLeeren();
            } catch (SQLException ex) {
                System.err.println(ex.getMessage());
            }

        }
    }//GEN-LAST:event_jButtonVeranstaltungAnlegenActionPerformed

    private void jButton_schliessenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_schliessenActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
    }//GEN-LAST:event_jButton_schliessenActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
                    new bzr_vaNeuVeranstaltung().setVisible(true);
                } catch (InstantiationException ex) {
                    Logger.getLogger(bzr_vaNeuVeranstaltung.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IllegalAccessException ex) {
                    Logger.getLogger(bzr_vaNeuVeranstaltung.class.getName()).log(Level.SEVERE, null, ex);
                } catch (SQLException ex) {
                    Logger.getLogger(bzr_vaNeuVeranstaltung.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonVeranstaltungAnlegen;
    private javax.swing.JButton jButton_schliessen;
    private javax.swing.JFormattedTextField jFormattedTextField_hnr;
    private javax.swing.JFormattedTextField jFormattedTextField_plz;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JSpinner jSpinnerEndDatum;
    private javax.swing.JSpinner jSpinnerStartDatum;
    private javax.swing.JTextField jTextField_Name;
    private javax.swing.JTextField jTextField_Ort;
    private javax.swing.JTextField jTextField_Straße;
    private javax.swing.JTextField jTextField_ZustandigeGeschaftsstelle;
    // End of variables declaration//GEN-END:variables

    private boolean prüfTextFields() {
        if (jTextField_Name.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "Sie müssen im Textfeld Name etwas eingeben");
            return false;
        }
        if (jTextField_Ort.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "Sie müssen im Textfeld Ort etwas eingeben");
            return false;
        }
        if (jFormattedTextField_plz.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "Sie müssen im Textfeld PLZ etwas eingeben");
            return false;
        }
        if (jFormattedTextField_plz.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "Sie müssen im Textfeld PLZ etwas eingeben");
            return false;
        }
        if (jTextField_Straße.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "Sie müssen im Textfeld Straße etwas eingeben");
            return false;
        }
        if (jFormattedTextField_hnr.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "Sie müssen im Textfeld Hausnummer etwas eingeben");
            return false;
        }
        if (jTextField_ZustandigeGeschaftsstelle.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "Sie müssen im Textfeld Zustänige Geschäftstelle etwas eingeben");
            return false;
        } else {
            return true;
        }


    }

    private void textfieldLeeren() {
        jTextField_Name.setText("");
        jTextField_Ort.setText("");
        jFormattedTextField_plz.setText("");
        jTextField_Straße.setText("");
        jFormattedTextField_hnr.setText("");
        jSpinnerStartDatum.setValue(new Date());
        jSpinnerEndDatum.setValue(new Date());
        jTextField_ZustandigeGeschaftsstelle.setText("");

    }

    private void initDatum() {
        JComponent editor_start = new JSpinner.DateEditor(jSpinnerStartDatum, "dd.MM.yyyy");
        jSpinnerStartDatum.setEditor(editor_start);
        JComponent editor_end = new JSpinner.DateEditor(jSpinnerEndDatum, "dd.MM.yyyy");
        jSpinnerEndDatum.setEditor(editor_end);
    }
}
